import { useLayoutEffect, Ref } from "react";
import { isArray } from "../utils";

const useImperativeHandle = (ref: Ref<any>, create: Function, inputs?: any) => {
  const nextInputs = isArray(inputs) ? inputs.concat([ref]) : null;
  useLayoutEffect(() => {
    if (typeof ref === "function") {
      ref(create());
      return () => ref(null);
    } else if (ref !== null) {
      // @ts-ignore
      ref.current = create();
      return () => {
        // @ts-ignore
        ref.current = null;
      };
    }
  }, nextInputs);
};
export default useImperativeHandle;
